<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账单名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入账单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账单总额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入账单总额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本月还款" prop="current">
        <el-input
          v-model="queryParams.current"
          placeholder="请输入本月还款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="还款日期" prop="payDate">
        <el-date-picker clearable
                        v-model="queryParams.payDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择还款日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="剩余还款" prop="remain">
        <el-input
          v-model="queryParams.remain"
          placeholder="请输入剩余还款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.bill_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['piaopiao:bill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['piaopiao:bill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['piaopiao:bill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['piaopiao:bill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table show-summary border height="540" v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账单名称" width="120" align="center" prop="name" />
      <el-table-column label="账单总额" align="center" prop="amount" />
      <el-table-column label="本月还款" align="center" prop="current" />
      <el-table-column label="还款日期" align="center" prop="payDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账单类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="剩余还款" align="center" prop="remain" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="修改日期" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column fixed="right" width="200" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handlePay(scope.row)"
            v-hasPermi="['piaopiao:bill:edit']"
          >还款</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-date"
            @click="handleRecord(scope.row)"
            v-hasPermi="['piaopiao:bill:edit']"
          >还款计划</el-button>
          <el-dropdown>
            <el-button size="mini" type="text">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['piaopiao:bill:edit']"
                >修改</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['piaopiao:bill:remove']"
                >删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改bill对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="!this.isAdd" label="id" prop="id">
          <el-input disabled v-model="form.id" placeholder="请输入id" />
        </el-form-item>
        <el-form-item label="账单名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入账单名称" />
        </el-form-item>
        <el-form-item label="账单总额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入账单总额" />
        </el-form-item>
        <el-form-item label="本月还款" prop="current">
          <el-input v-model="form.current" placeholder="请输入本月还款" />
        </el-form-item>
        <el-form-item label="还款日期" prop="payDate">
          <el-date-picker clearable
                          v-model="form.payDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择还款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账单类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.bill_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="剩余还款" prop="remain">
          <el-input v-model="form.remain" placeholder="请输入剩余还款" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.bill_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog  :visible.sync="payOpen" width="500px" append-to-body title="还款">
      <el-row :gutter="15">
        <el-form ref="payForm" :model="payFormDate" :rules="rules" size="medium" label-width="100px"
                 label-position="left">
          <el-col :span="24">
            <el-form-item label="id" prop="id">
              <el-input v-model="payFormDate.id" placeholder="请输入id" :maxlength="11" show-word-limit readonly
                        clearable prefix-icon='el-icon-mobile' :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否还清" prop="isComplete">
              <el-switch v-model="isComplete"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label-width="102px" label="还款金额" prop="current">
              <el-input-number :disabled="isComplete"  v-model="payFormDate.current" placeholder="还款金额" :precision='2'>
              </el-input-number>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="payClose">取消</el-button>
        <el-button type="primary" @click="handlePayConfirm">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog  :visible.sync="recordOpen" @opened="getRecordList" width="1000px" append-to-body title="还款记录">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleRecordAdd"
            v-hasPermi="['piaopiao:bill:add']"
          >新增</el-button>
        </el-col>
      </el-row>
      <el-table height="540" v-loading="loading1" :data="recordList">
        <el-table-column label="计划名称" width="250" align="center" prop="name" />
        <el-table-column label="还款金额" align="center" prop="amount" />
        <el-table-column label="还款月份" align="center" prop="month" />
        <el-table-column label="还款时间" align="center" prop="payDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total1>0"
        :total="total1"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="getRecordList"
      />
      <el-dialog  :visible.sync="recordFormOpen" width="500px" append-to-body title="还款">
        <el-row :gutter="15">
          <el-form ref="recordForm" :model="recordFormDate" :rules="rules" size="medium" label-width="100px"
                   label-position="left">
            <el-form-item label="账单名称" prop="name">
              <el-input v-model="recordFormDate.name" placeholder="请输入账单名称" />
            </el-form-item>
            <el-form-item label="账单总额" prop="amount">
              <el-input v-model="recordFormDate.amount" placeholder="请输入账单总额" />
            </el-form-item>
            <el-form-item label="月份" prop="month" required>
              <el-input-number v-model="recordFormDate.month" placeholder="请输入月份"/>
            </el-form-item>
            <el-form-item label="还款日期" prop="payDate">
              <el-date-picker clearable
                              v-model="recordFormDate.payDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择还款日期">
              </el-date-picker>
            </el-form-item>
          </el-form>
        </el-row>
        <div slot="footer">
          <el-button @click="recordClose">取消</el-button>
          <el-button type="primary" @click="handleRecordConfirm">确定</el-button>
        </div>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import { listBill, getBill, delBill, addBill, updateBill,addRecord,updateRecord,recordList,pay } from "@/api/piaopiao/bill"

export default {
  name: "Bill",
  dicts: ['bill_state','bill_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      loading1: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1: 0,
      // bill表格数据
      billList: [],
      // record表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      payOpen: false,
      recordOpen:false,
      recordFormOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        amount: null,
        current: null,
        payDate: null,
        type: null,
        remain: null,
        state: null
      },
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        billId:null
      },
      // 表单参数
      form: {},
      payFormDate:{},
      recordFormDate:{},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "账单名称不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "账单总额不能为空", trigger: "blur" }
        ],
        current: [
          { required: true, message: "本月还款不能为空", trigger: "blur" }
        ],
        payDate: [
          { required: true, message: "还款日期不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "账单类型不能为空", trigger: "change" }
        ],
        remain: [
          { required: true, message: "剩余还款不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
    },
      isAdd:false,
      isComplete:true,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询bill列表 */
    getList() {
      this.loading = true
      listBill(this.queryParams).then(response => {
        this.billList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getRecordList(billId) {
      return new Promise((resolve, reject) => {
        this.loading1 = true
        recordList(this.queryParams1).then(response => {
          this.total1 = response.total
          this.recordList = response.rows
          this.loading1 = false
          resolve()
        })
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        amount: null,
        current: null,
        payDate: null,
        type: null,
        remain: null,
        state: null
      }
      this.resetForm("form")
    },
    // 表单重置
    resetPay() {
      this.payFormDate = {
        id: null,
        amount: null,
        isComplete: true,
      }
      this.resetForm("form")
    },
    resetRecordPay() {
      this.recordFormDate = {
        name:null,
        amount: null,
        month: null,
        payDate: null,
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.isAdd =  true
      this.title = "添加bill"
    },
    handleRecordAdd() {
      this.resetRecordPay()
      this.recordFormOpen = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.isAdd = false
      const id = row.id || this.ids
      getBill(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改bill"
      })
    },
    handlePay(row) {
      this.resetPay()
      this.isComplete = true
      const id = row.id || this.ids
        getBill(id).then(response => {
          this.payFormDate = response.data
          this.$nextTick(()=>{
            this.payOpen = true
          })
        })
    },
    async handleRecord(row) {
      const billId = row.id
      this.queryParams1.billId = billId
      this.recordOpen = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addBill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除bill编号为"' + ids + '"的数据项？').then(function() {
        return delBill(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('piaopiao/bill/export', {
        ...this.queryParams
      }, `bill_${new Date().getTime()}.xlsx`)
    },
    payClose() {
      this.payOpen = false
    },
    recordClose() {
      this.recordFormOpen = false
    },
    handlePayConfirm() {
      this.$refs["payForm"].validate(valid => {
        if (valid) {
          if (this.payFormDate.id != null) {
            pay(this.payFormDate).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.payOpen = false
              this.getList()
            })
          }
        }
      })
    },
    handleRecordConfirm() {
      this.$refs["recordForm"].validate(valid => {
        if (valid) {
          this.recordFormDate.billId = this.queryParams1.billId
          addRecord(this.recordFormDate).then(response => {
            this.$modal.msgSuccess("新增成功")
            this.recordFormOpen = false
            this.getRecordList()
          })
        }
      })
    }
  }
}
</script>
